// pages/bill/bill.js
import Toast from '../../miniprogram/miniprogram_npm/@vant/weapp/toast/toast';
const request=require('../../util/request.js')
const navBarHeight = wx.getSystemInfoSync().statusBarHeight + 44;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taptime:0,
    navBarHeight:navBarHeight,
    showbill:false,
    createShow:false,
    book_name:"",
    book_id:0,
    mindate:Math.round((new Date() - 365*86400*1000)),
    date:"",
    show:false,
    books:[],
    currentBook:"默认账本",
    currentBookIndex:0,
    logs:[],
    id:0
  },
  onDisplay() {
    console.log(111)
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
    });
    this.getLogs(this.data.date)
  },
  selectBook(e){
    if (this.data.taplock){
      this.setData({
        currentBookIndex:e.currentTarget.dataset.index,
        currentBook:this.data.books[e.currentTarget.dataset.index].name
      })
      this.getLogs();
      this.switchShowBill({})
    }
  },
   // 触摸开始
   touchStart() {
    this.data.taplock = true  //打开单击
    this.data.longtap = false //初始化长按事件触发状态
    console.log("触摸开始")
  },
  // 触摸结束
  touchEnd() {
    if (this.data.longtap)
      console.log("触摸结束") //如果长按触发了，那么正常结束就好了
    else
      this.data.taplock = true //如果长按没触发，说明是单击，打开单击锁
  },
  getLogs(date=""){
   
      var id = this.data.books[this.data.currentBookIndex].id
      this.setData({
        id:id
      })
      var that = this;
      request.requestAjax('books/getLogs',{date:date,book_id:id},"加载中",function(res){
          if(res.status==200){
              that.setData({
                logs:res.data.logs,
                range:res.data.range
              });
              console.log(that.data.books)
  
              return false;
          }else{
            wx.showToast({
              title: '请求失败',
              icon:"none"
            })
          }
    
      },function(res){
        wx.showToast({
          title: '请求失败',
          icon:"none"
        })
      });
  },
  switchShowBill(e){
    this.setData({
      showbill:!this.data.showbill
    })
    if(!this.data.showbill)this.closeCreate();
  },
  bindKeyInputVant(event){
    // console.log(event);
    let valueName = event.target.dataset.variable;
    let name = `formData.${valueName}`;
    this.setData({
      [valueName]:event.detail
    });
    // console.log(this.data)
  },
  closeCreate(){
    this.setData({
      createShow:false
    })
    var that =this;
    setTimeout(function(){
      that.setData({
        book_name:"",
        book_id:0
      })
    },300)
  },
  delBook(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认移除此账本吗？\n移除后，此账本下的记录会自动转移至主账本！',
      success (res) {
        if (res.confirm) {
          that.removeBook();
        } else if (res.cancel) {
          
        }
      }
    })

  },
  removeBook(){
    var that = this;
    
    request.requestAjax('company/removeBook',{id:that.data.book_id},"加载中",function(res){
      if(res.status==200){
        wx.showToast({
          title: '删除成功',
          icon:"success"
        })
        that.setData({
          currentBookIndex:0
        })
        that.getBooks();

        that.closeCreate();

        
        return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
        that.closeCreate();
      }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
      that.closeCreate();
    });
 
  },
  createbook(){
    this.setData({
      createShow:true
    })
  },
  getBooks(){
    var that = this;
    request.requestAjax('company/getBooks',{},"加载中",function(res){
        if(res.status==200){
            that.setData({
              books:res.data.books,
              id:res.data.default_book_id
            });
            console.log(that.data.currentBookIndex)
            console.log(that.data.books)
            that.setData({
            
              currentBook:that.data.books[that.data.currentBookIndex].name
            });

            that.getLogs();
            return false;
        }else{
          wx.showToast({
            title: '请求失败',
            icon:"none"
          })
        }
  
    },function(res){
      wx.showToast({
        title: '请求失败',
        icon:"none"
      })
    });
  },
  saveBook(){
    var that = this;
    
    request.requestAjax('company/saveAccountBook',{name:that.data.book_name,id:that.data.book_id},"加载中",function(res){
      if(res.status==200){
        wx.showToast({
          title: '保存成功',
          icon:"success"
        })
        that.getBooks();
      
        that.closeCreate();

        
        return false;
      }else{
        wx.showToast({
          title: res.msg,
          icon:"none"
        })
        that.closeCreate();
      }

    },function(res){
      wx.showToast({
        title: '提交失败',
        icon:"none"
      })
      that.closeCreate();
    });
  },
  //touch 
  editBook(e) {
    this.setData({
      book_id:e.currentTarget.dataset.id,
      book_name:this.data.books[e.currentTarget.dataset.index].name,
      currentBookIndex:e.currentTarget.dataset.index,
      currentBook:this.data.books[e.currentTarget.dataset.index].name,
      createShow:true
    })
    this.getLogs()
    this.setData({
      taplock:false,
      longtap:true
    })
  }, 


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onShow(){
    this.getBooks();
  },
  goPage(event){
    // console.log(event);
    if(event.currentTarget.dataset.pageurl=="/pages/register/register"){
      Toast('暂未开放注册~');
    }

    wx.navigateTo({
      url: event.currentTarget.dataset.pageurl,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})